<?php
session_start();
require_once 'include/init.php';
require_once ROOT_DIR . 'include/lib/weibooauth.php';
$act = isset($_REQUEST['act']) ? trim($_REQUEST['act']) : '';

$client = new WeiboClient( WB_AKEY , WB_SKEY , $_SESSION['last_key']['oauth_token'] , $_SESSION['last_key']['oauth_token_secret']  );
$me = $client->verify_credentials();
$name = isset($me['screen_name']) ? $me['screen_name'] : Util::msgRedirect('没有授权', 'index.php');
$uid = isset($me['id']) ? intval($me['id']) : Util::msgRedirect('没有授权', 'index.php');
$allowType = array(
	'image/jpeg',
	'image/pjpeg',
	'image/png',
);

if ($act=='pub'){
	
	$img = isset($_FILES['img']) && $_FILES['img'] ? $_FILES['img'] : Util::msgRedirect('没有选择图片');
	$idAry = isset($_POST['ids']) && $_POST['ids'] && is_array($_POST['ids']) ? $_POST['ids'] : Util::msgRedirect('请填写运动员号码');
	foreach ($idAry as $key=>$id) {
		if (!$id){
			unset($idAry[$key]);
		}
		if (strlen($id)>20){
			Util::msgRedirect('运动员号码不会有这么长的吧，如果您确定正确请＠终小南');
		}
	}
	$idAry = array_unique($idAry);
	if (!$idAry) Util::msgRedirect('请填写运动员号码');
	$text = isset($_POST['text']) && $_POST['text'] ? $_POST['text'] : '';
	
	if (!in_array(strtolower($img['type']), $allowType)){
		Util::msgRedirect('请选择jpg或者png格式的图片');
	}
	if ($img['size']>5242880){
		Util::msgRedirect('图片尺寸不能超过5M,请您在压缩后再上传');
	}
	$ext = getExt($img['name']);
	$filename = md5(time() . microtime() . rand(0, 99999)) . '.' . $ext;
	$savePath = 'upload/' . dechex(rand(1, 255));
	if (!is_dir($savePath)){
		mkdir($savePath);
	}
	$filePath = $savePath . '/' . $filename;
	if (move_uploaded_file($img['tmp_name'], $filePath)){
		$filename = 'http://jiemo.net/' . $filePath;
	}else{
		Util::msgRedirect('图片上传失败');
	}
	$text = mb_substr($text, 0, 60);
	$text .= ' ' .implode(',', $idAry) . "号运动员，快来认领你的照片吧！http://www.jiemo.net";
	
	$result = $client->upload($text, $filename);

	$weiboRow['sid'] = $result['id'];
	$weiboRow['uname'] = isset($result['user']['screen_name']) ? $result['user']['screen_name'] : $result['user']['name'];
	$weiboRow['uid'] = $result['user']['id'];
	$weiboRow['thumbnail_pic'] = $result['thumbnail_pic'];
	$weiboRow['bmiddle_pic'] = $result['bmiddle_pic'];
	$weiboRow['original_pic'] = $result['original_pic'];
	$weiboRow['local_pic'] = $filePath;
	$weiboRow['text'] = $result['text'];
	$weiboRow['runnerid'] = implode(',', $idAry);
	require_once 'module/RunnerPic.php';
	require_once 'module/Weibo.php';
	$weiboObj = new Weibo();
	$runnerpicObj = new RunnerPic();
	$weibo_id = $weiboObj->addWeibo($weiboRow);
	$runnerpicObj->addRunnerPic($idAry, $weibo_id);
	Util::msgRedirect('添加成功', 'pubpic.php');
	
}else{

	require_once ROOT_DIR . 'include/lib/weibooauth.php';
	
	$tpl->display('pubpic.tpl');

}
exit();

function showmsg($msg)
{
	echo "<script type='text/javascript'>alert('{$msg}');history.back();</script>";
	exit();
}
function getExt($filename)
{
	$pointPos = strrpos($filename, '.');
	return substr($filename, $pointPos+1);
}

function stripExt($filename)
{
	$pointPos = strrpos($filename, '.');
	return substr($filename, 0, $pointPos);
}